package com.paohe.lianxin_pda.production_line.chengpPei;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.snackbar.Snackbar;
import com.paohe.lianxin_pda.R;

import java.util.ArrayList;
import java.util.List;

public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.MyViewHolder> {
    private Context context;
    private List<String> list;
    private List<String[]> sonList = new ArrayList<String[]>();


    public RecycleAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public RecycleAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                context).inflate(R.layout.fragment_father_item, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecycleAdapter.MyViewHolder holder, final int position) {
        holder.tv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list.size() == 1) {
                    Snackbar.make(v, "此条目不能删除", Snackbar.LENGTH_SHORT).show();
                } else {
                    //               删除自带默认动画
                    removeData(position);
                }
            }
        });

        //开始列表的二级且套
        RecyclerView rvCategory = holder.son_recycler_view;
        rvCategory.setLayoutManager(new LinearLayoutManager(context));
        sonList = initData();
        RecycleSonAdapter sonAdapter = new RecycleSonAdapter(context,sonList, rvCategory);
        rvCategory.setAdapter(sonAdapter);
        //更新数据后定位到最底部
//        rvCategory.smoothScrollToPosition(sonAdapter.getItemCount()-1);


    }
    protected ArrayList<String[]> initData() {
        ArrayList<String[]> mDatas = new ArrayList<>();
        String[] addObj =new String[]{"","","","","提交","#2C89E9","1"};
        mDatas.add(addObj);
        return mDatas;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    //  添加数据
    public void addData(int position) {
//      在list中添加数据，并通知条目加入一条
        list.add(position, "我是商品" + position);
        //添加动画
        notifyItemInserted(position);
    }
    //  删除数据
    public void removeData(int position) {
        list.remove(position);
        //删除动画
        notifyItemRemoved(position);
        notifyDataSetChanged();
    }

    /**
     * ViewHolder的类，用于缓存控件
     */
    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_delete;
        RecyclerView son_recycler_view;
        //因为删除有可能会删除中间条目，然后会造成角标越界，所以必须整体刷新一下！
        public MyViewHolder(View view) {
            super(view);
            tv_delete = view.findViewById(R.id.tv_delete);
            son_recycler_view = view.findViewById(R.id.son_recycler_view);

        }
    }
}
